home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / packagetool / sample package / htmlsample sources / history.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  10.3 KB  |  295 lines

  1. /*
  2.     File: History.c
  3.     
  4.     Description:
  5.         These routines are used to store visitled links.
  6.     
  7.     HTMLSample is an application illustrating how to use the new
  8.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  9.     is Apple's light-weight HTML rendering engine capable of
  10.     displaying HTML files.
  11.  
  12.     Copyright:
  13.         © Copyright 1999 Apple Computer, Inc. All rights reserved.
  14.     
  15.     Disclaimer:
  16.         IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  17.         ("Apple") in consideration of your agreement to the following terms, and your
  18.         use, installation, modification or redistribution of this Apple software
  19.         constitutes acceptance of these terms.  If you do not agree with these terms,
  20.         please do not use, install, modify or redistribute this Apple software.
  21.  
  22.         In consideration of your agreement to abide by the following terms, and subject
  23.         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  24.         copyrights in this original Apple software (the "Apple Software"), to use,
  25.         reproduce, modify and redistribute the Apple Software, with or without
  26.         modifications, in source and/or binary forms; provided that if you redistribute
  27.         the Apple Software in its entirety and without modifications, you must retain
  28.         this notice and the following text and disclaimers in all such redistributions of
  29.         the Apple Software.  Neither the name, trademarks, service marks or logos of
  30.         Apple Computer, Inc. may be used to endorse or promote products derived from the
  31.         Apple Software without specific prior written permission from Apple.  Except as
  32.         expressly stated in this notice, no other rights or licenses, express or implied,
  33.         are granted by Apple herein, including but not limited to any patent rights that
  34.         may be infringed by your derivative works or by other works in which the Apple
  35.         Software may be incorporated.
  36.  
  37.         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  38.         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  39.         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40.         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  41.         COMBINATION WITH YOUR PRODUCTS.
  42.  
  43.         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  44.         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  45.         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46.         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  47.         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  48.         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  49.         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50.  
  51.     Change History (most recent first):
  52.         Wed, Dec 22, 1999 -- created
  53. */
  54.  
  55.  
  56. #include "History.h"
  57. #include <stddef.h>
  58. #include <Memory.h>
  59. #include <TextUtils.h>
  60. #include <Errors.h>
  61. #include <string.h>
  62.  
  63.  
  64.  
  65. /* individual elements in the history are stored in a linked
  66.     list.  it's a two way list for easy insertions and deletions. */
  67. typedef struct HistoryStruct HistoryRecord;
  68. typedef HistoryRecord *HistoryPtr, **HistoryHandle;
  69. struct HistoryStruct {
  70.     HistoryHandle prev, next;
  71.     char url[1];
  72.     unsigned char printName[1];
  73. };
  74.  
  75. /* the history data handle contains a list of history
  76.     elements along with a pointer to the current
  77.     element being displayed. */
  78. struct HistoryData {
  79.     HistoryHandle first, last;
  80.     HistoryHandle current;
  81. };
  82.  
  83.  
  84.  
  85. /* NewHistory creats a new history and returns
  86.     a handle to it. Here, we just return an handled
  87.     set to zero. */
  88. HistoryDataHandle NewHistory(void) {
  89.     return (HistoryDataHandle) NewHandleClear(sizeof(HistoryData));
  90. }
  91.  
  92.  
  93. /* DisposeHistory disposes of a history and all of the
  94.     structures allocated for it. */
  95. void DisposeHistory(HistoryDataHandle hd) {
  96.     HistoryHandle rover, temp;
  97.     rover = (**hd).first;
  98.         /* delete all of the elements in the history list */
  99.     while (rover != NULL) {
  100.         temp = rover;
  101.         rover = (**rover).next;
  102.         DisposeHandle((Handle) temp);
  103.     }
  104.     DisposeHandle((Handle) hd);
  105. }
  106.  
  107.  
  108. /* AddToHistory adds a new element to the history.  Both
  109.     the URL and the printed representation of its url
  110.     are stored.  NOTE:  if we have called GoBack a few times
  111.     before this call, then those previously viewed items
  112.     are removed from the history. This is so if we choose
  113.     GoBack again, then we will arrive at the same link we
  114.     are looking at now.  */
  115. OSErr AddToHistory(HistoryDataHandle hd, char const* url, StringPtr printName) {
  116.     HistoryHandle hrec, rover;
  117.     long strbytes;
  118.         /* create a new history record */
  119.     strbytes = strlen(url);
  120.     hrec = (HistoryHandle) NewHandle(offsetof(HistoryRecord, url) + strbytes + 1 + printName[0] + 1);
  121.     if (hrec == NULL) return memFullErr;
  122.     BlockMoveData(url, (**hrec).url, strbytes + 1);
  123.     BlockMoveData(printName, (**hrec).url + strbytes + 1, printName[0] + 1);
  124.     (**hrec).prev = (**hrec).next = NULL;
  125.         /* clear out records after current.  We do this
  126.         so the next time we choose the go back command,
  127.         we'll arrive at the same one that was the current
  128.         one before this routine was called.  Essentially, the
  129.         user is conducting a depth first search of the html,
  130.         and here we are branching.  */
  131.     while ((**hd).current != (**hd).last && (**hd).current != NULL) {
  132.         rover = (**(**hd).current).next;
  133.         if ((**rover).next != NULL)
  134.             (**(**rover).next).prev = (**rover).prev;
  135.         else (**hd).last = (**rover).prev;
  136.         if ((**rover).prev != NULL)
  137.             (**(**rover).prev).next = (**rover).next;
  138.         else (**hd).first = (**rover).next;
  139.         DisposeHandle((Handle) rover);
  140.     }
  141.         /* add the new record to the end of the list */
  142.     if ((**hd).first == NULL)
  143.         (**hd).first = (**hd).last = hrec;
  144.     else {
  145.         (**hrec).prev = (**hd).last;
  146.         (**(**hd).last).next = hrec;
  147.         (**hd).last = hrec;
  148.     }
  149.     (**hd).current = hrec;
  150.     return noErr;
  151. }
  152.  
  153.  
  154. /* InHistory returns true if the URL is among the urls
  155.     currently stored in the history. */
  156. Boolean InHistory(HistoryDataHandle hd, char const* url) {
  157.     HistoryHandle rover;
  158.     Boolean isequal;
  159.     for (rover = (**hd).first; rover != NULL; rover = (**rover).next) {
  160.         HLock((Handle) rover);
  161.         isequal = (strcmp(url, (**rover).url) == 0);
  162.         HUnlock((Handle) rover);
  163.         if (isequal) return true;
  164.     }
  165.     return false;
  166. }
  167.  
  168.  
  169. /* CanGoBack returns true if it makes sense to call the
  170.     GoBack command.  i.e. if there are one or more links
  171.     in the history beyond the current one. */
  172. Boolean CanGoBack(HistoryDataHandle hd) {
  173.     if ((**hd).current == NULL)
  174.         return false;
  175.     else return ((**(**hd).current).prev != NULL);
  176. }
  177.  
  178.  
  179. /* GoBack copies the previous url in the history
  180.     into a new handle and returns that handle in
  181.     *url.  It is the caller's responsibility to dispose
  182.     of the handle after it has been used. */
  183. OSErr GoBack(HistoryDataHandle hd, Handle *url) {
  184.     OSErr err;
  185.     HistoryHandle nextcurrent;
  186.     if ((**hd).current == NULL) return paramErr;
  187.     nextcurrent = (**(**hd).current).prev;
  188.     HLock((Handle) nextcurrent);
  189.     err = PtrToHand((**nextcurrent).url, url, strlen((**nextcurrent).url) + 1);
  190.     HUnlock((Handle) nextcurrent);
  191.     if (err == noErr)
  192.         (**hd).current = nextcurrent;
  193.     return err;
  194. }
  195.  
  196.  
  197. /* CanGoForward returns true if it makes sense to call the
  198.     GoForward command.  i.e. if there are one or more links
  199.     in the history ahead of the current one.  This can only
  200.     happen after the user has chosen GoBack one or more
  201.     times. */
  202. Boolean CanGoForward(HistoryDataHandle hd) {
  203.     if ((**hd).current == NULL)
  204.         return false;
  205.     else return ((**(**hd).current).next != NULL);
  206. }
  207.  
  208.  
  209. /* GoForward copies the next url in the history
  210.     into a new handle and returns that handle in
  211.     *url.  It is the caller's responsibility to dispose
  212.     of the handle after it has been used. */
  213. OSErr GoForward(HistoryDataHandle hd, Handle *url) {
  214.     OSErr err;
  215.     HistoryHandle nextcurrent;
  216.     if ((**hd).current == NULL) return paramErr;
  217.     nextcurrent = (**(**hd).current).next;
  218.     HLock((Handle) nextcurrent);
  219.     err = PtrToHand((**nextcurrent).url, url, strlen((**nextcurrent).url) + 1);
  220.     HUnlock((Handle) nextcurrent);
  221.     if (err == noErr)
  222.         (**hd).current = nextcurrent;
  223.     return err;
  224. }
  225.  
  226.  
  227. /* CanGoHome returns true if it makes sense to call the
  228.     GoHome command.  i.e. if there are one or more links
  229.     in the history.  This can only happen after AddToHistory
  230.     has been called one or more times. */
  231. Boolean CanGoHome(HistoryDataHandle hd) {
  232.     return ((**hd).first != (**hd).current) && ((**hd).current != NULL);
  233. }
  234.  
  235.  
  236. /* GoBack copies the first url in the history
  237.     into a new handle and returns that handle in
  238.     *url.  It is the caller's responsibility to dispose
  239.     of the handle after it has been used. */
  240. OSErr GoHome(HistoryDataHandle hd, Handle *url) {
  241.     OSErr err;
  242.     if ((**hd).first == NULL) return paramErr;
  243.     HLock((Handle) (**hd).first);
  244.     err = PtrToHand((**(**hd).first).url, url, strlen((**(**hd).first).url) + 1);
  245.     HUnlock((Handle) (**hd).first);
  246.     if (err == noErr)
  247.         (**hd).current = (**hd).first;
  248.     return err;
  249. }
  250.  
  251.  
  252. /* AppendHistoryToMenu rebuilds the Go menu adding items to the
  253.     bottom of the menu according to the items in the
  254.     history.  The names of the items are the same as
  255.     the printNames provided in the AddToHistory command. */
  256. OSErr AppendHistoryToMenu(HistoryDataHandle hd, MenuHandle theMenu) {
  257.     HistoryHandle rover;
  258.     Str255 title;
  259.     unsigned char *titlep;
  260.     if ((**hd).last == NULL) return noErr;
  261.     AppendMenu(theMenu, "\p(-");
  262.     for (rover = (**hd).last; rover != NULL; rover = (**rover).prev) {
  263.         HUnlock((Handle) rover);
  264.         titlep = (unsigned char *) ((**rover).url + strlen((**rover).url) + 1);
  265.         BlockMoveData(titlep, title, titlep[0] + 1);
  266.         AppendMenu(theMenu, title);
  267.         HUnlock((Handle) rover);
  268.     }
  269.     return noErr;
  270. }
  271.  
  272.  
  273. /* GoToMenuItem copies the itemIndex'th url in the history
  274.     into a new handle and returns that handle in
  275.     *url.  It is the caller's responsibility to dispose
  276.     of the handle after it has been used.  This routine
  277.     should only be called after a menu selection has
  278.     been made in a menu built by AppendHistoryToMenu.  */
  279. OSErr GoToMenuItem(HistoryDataHandle hd, Handle *url, short itemIndex) {
  280.     HistoryHandle rover;
  281.     short i;
  282.     OSErr err;
  283.     if ((**hd).last == NULL) return paramErr;
  284.     for (i = 1, rover = (**hd).last; rover != NULL; rover = (**rover).prev, i++) {
  285.         if (i == itemIndex) {
  286.             HLock((Handle) rover);
  287.             err = PtrToHand((**rover).url, url, strlen((**rover).url) + 1);
  288.             HUnlock((Handle) rover);
  289.             if (err == noErr)
  290.                 (**hd).current = rover;
  291.             return err;
  292.         }
  293.     }
  294.     return noErr;
  295. }